import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.List;

public class Server extends JFrame {
    JButton btn = new JButton("清除");

    private ArrayList<String> account=new ArrayList<>();    //用户列表
    private Map<String, MulticastSocket> userSocketMap = new HashMap<>();      //用户名与对应socket形成键值对
    public Server() throws IOException {
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(btn, "North");
        setTitle("服务器画板");
        setSize(600, 400);
        setVisible(true);
        receiveAndSend();
    }

    //转发
    public void receiveAndSend() throws IOException{
        MulticastSocket sSocket = null;
        DatagramPacket receivePacket = null;
        int port = 12000; //端口号
        byte[] receive = new byte[1000];//接收客户端消息
        String cin=null;//接受客户端消息
        String cin2=null;
        sSocket = new MulticastSocket(port);
        InetAddress group=InetAddress.getByName("230.0.0.1");
        sSocket.joinGroup(group);
        receivePacket = new DatagramPacket(receive,receive.length);
        while(true){
            sSocket.receive(receivePacket);
            cin = new String(receivePacket.getData(),0,receivePacket.getLength());
            StringTokenizer scin = new StringTokenizer(cin, " ");
            String orderStr = scin.nextToken();//接受指令
            if(scin.hasMoreElements()) {
                scin.nextToken();
                scin.nextToken();
            }

            String orderStr2=null;
            if(!Objects.equals(orderStr, "btn")) {
                sSocket.receive(receivePacket);
                cin2 = new String(receivePacket.getData(), 0, receivePacket.getLength());
                StringTokenizer scin2 = new StringTokenizer(cin2, " ");
                orderStr2 = scin2.nextToken();//接受指令
                if(scin2.hasMoreElements()) {
                    scin2.nextToken();
                    scin2.nextToken();
                }
            }
            //转发消息
            sendDataToAllUsers(cin);
            sendDataToAllUsers(cin2);
            switch (orderStr){
                case "SHARE":
                    account.add(orderStr2);
                    userSocketMap.put(orderStr2,sSocket);
                    break;
                case "STOPSHARE":
                    account.remove(orderStr2);
                    userSocketMap.remove(orderStr2);
                    break;
            }
        }
    }
    public void sendDataToAllUsers(String data) throws IOException {
        for (Map.Entry<String, MulticastSocket> entry : userSocketMap.entrySet()){
            MulticastSocket socket=entry.getValue();
            sendDataToUser(data,socket);
        }
    }
    public void sendDataToUser(String data,MulticastSocket socket)throws IOException{
        DatagramPacket cPacket = null;
        InputStreamReader out = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(out);
        InetAddress group = InetAddress.getByName("230.0.0.1");// 获取ip地址
//        int clientPort = socket.getLocalPort();
        int clientPort = 13000;
        byte[] sendData = data.getBytes(StandardCharsets.UTF_8);
        cPacket =new DatagramPacket(sendData,sendData.length,group,clientPort);
        socket.send(cPacket);
    }

    public static void main(String[] args) throws IOException {
        new Server();
    }
}


